package me.zk;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * @Author: zhang kai
 * @Date: 2022/07/12
 * @Description:
 */
public class Demo4 {
    public static void main(String[] args) {
        Function<Integer, Integer> f = x -> x + 1;
        Function<Integer, Integer> g = x -> x * 2;
        Function<Integer, Integer> h = f.andThen(g);
        int result = h.apply(1);//4
        System.out.println(result);//4
        List<String> title = Arrays.asList("Java8", "In", "Action");
        Stream<String> s = title.stream();
        s.forEach(System.out::println);
        //java.lang.IllegalStateException
//        s.forEach(System.out::println);
        Stream<String> stream = Stream.of("Java 8 ", "Lambdas ", "In ", "Action");
        stream.map(String::toUpperCase).forEach(System.out::println);
        int[] numbers = {2, 3, 5, 7, 11, 13};
        int sum = Arrays.stream(numbers).sum();
        long uniqueWords = 0;
        try(Stream<String> lines =
                    Files.lines(Paths.get("data.txt"), Charset.defaultCharset())){
            uniqueWords = lines.flatMap(line -> Arrays.stream(line.split(" ")))
                    .distinct()
                    .count();
        }
        catch(IOException e){

        }
        Stream.iterate(0, n -> n + 2)
                .limit(10)
                .forEach(System.out::println);

       String s1 =  Arrays.asList("Java8", "In", "Action").stream().reduce("",(a,b) -> a+b);
       System.out.println(s1);
       Optional<String> o =  Arrays.asList("Java8", "In", "Action").stream().reduce((a, b) -> a.length() > b.length() ? a:b);
       System.out.println(o.get());
    }
}
